package com.orcs.leefei.utils.kotlin

import android.graphics.Color

fun Int.darken(fraction: Float): Int {
  return blendColors(Color.BLACK, this, fraction)
}

fun Int.lighten(fraction: Float): Int {
  return blendColors(Color.WHITE, this, fraction)
}

/**
 * @return luma value according to to YIQ color space.
 */
fun Int.calculateYiqLuma(): Int {
  return Math.round((299 * Color.red(this) +
      587 * Color.green(this) +
      114 * Color.blue(this)) / 1000f)
}

/**
 * Blend `color1` and `color2` using the given ratio.

 * @param ratio of which to blend. 1.0 will return `color1`, 0.5 will give an even blend,
 * *              0.0 will return `color2`.
 */
fun blendColors(color1: Int, color2: Int, ratio: Float): Int {
  val inverseRatio = 1f - ratio
  val r = Color.red(color1) * ratio + Color.red(color2) * inverseRatio
  val g = Color.green(color1) * ratio + Color.green(color2) * inverseRatio
  val b = Color.blue(color1) * ratio + Color.blue(color2) * inverseRatio
  return Color.rgb(r.toInt(), g.toInt(), b.toInt())
}

fun Int.changeBrightness(fraction: Float): Int {
  return if (this.calculateYiqLuma() >= 128) this.darken(fraction)
  else this.lighten(fraction)
}

fun Int.modifyAlpha(alpha: Int): Int {
  return this and 0x00ffffff or (alpha shl 24)
}

//inline fun Int.materialVibrantColor():Int{
//  return vibrantColors[this%vibrantColors.size]
//}
//
//val vibrantColors = mutableListOf(
//    0xFF4fc3f7.toInt(),
//    0xFFfff176.toInt(),
//    0xFFaed581.toInt(),
//    0xFFffd54f.toInt(),
//    0xFF42bd41.toInt(),
//    0xFF9575cd.toInt(),
//    0xFFf36c60.toInt()
//)